package com.zsjt.app.service.impl;

import com.zsjt.app.compoent.utils.email.EmailCode;
import com.zsjt.app.compoent.utils.phone.PhoneCode;
import com.zsjt.app.service.SendCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Service;

@Slf4j
@Service("sendCodeService")
public class SendCodeServiceImpl implements SendCodeService {

    private PhoneCode phoneCode;

    private EmailCode emailCode;

    @Autowired
    @Qualifier("emailCode")
    public void setEmailCode(EmailCode emailCode) {
        this.emailCode = emailCode;
    }

    @Autowired
    @Qualifier("phoneCode")
    public void setPhoneCode(PhoneCode phoneCode) {
        this.phoneCode = phoneCode;
    }

    //@Async  //异步方法
    @Override
    public String sendPhoneCode(String phone) {
        String code = phoneCode.getPhoneCode();
        try {
            //phoneCode.sendPhoneCode(phone,code);
            log.info("app系统给"+phone+"发送验证码"+code+"成功");
            return code;
        }catch (Exception e){
            log.error("app系统给"+phone+"发送验证码失败");
            return null;
        }
    }

   // @Async  //异步方法
    @Override
    public String sendEmailCode(String email){
        String code =emailCode.getEmailCode();
        try{
            emailCode.sendEmailCode(email,code);
            log.info("app系统给"+email+"发送验证码"+code+"成功");
        }catch (MailException e){
            log.info("app系统给"+email+"发送验证码失败");
            e.printStackTrace();
        }
        return code;
    }

}
